package com.example.droolstool.config;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * @Author 赵云龙
 * @Date 2020/6/7
 * @Description TODO
 */
@Configuration
public class DroolsRuleConfig {
//    private static final Logger LOGGER = LoggerFactory.getLogger(DroolsRuleConfig.class);
//    private static final String RULES_PATH = "droolRule/";
//    private final KieServices kieServices = KieServices.Factory.get();
//private static final String drlFile = "TAXI_FARE_RULE.drl";
//
//    @Bean
//    public KieFileSystem kieFileSystem() throws IOException {
//        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
//        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//        Resource[] files = resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");
//        String path = null;
//        for (Resource file : files) {
//            path = RULES_PATH + file.getFilename();
//            LOGGER.info("path=" + path);
//            kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
//        }
//        return kieFileSystem;
//    }
//
//    @Bean
//    public KieContainer kieContainer() throws IOException {
//        KieRepository kieRepository = kieServices.getRepository();
//        kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
//        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
//        kieBuilder.buildAll();
//        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
//    }
//
//    @Bean
//    public KieBase kieBase() throws IOException {
//        return kieContainer().getKieBase();
//    }
//
//    @Bean
//    public KieSession kieSession() throws IOException {
//        return kieContainer().newKieSession();
//    }
//
//    @Bean
//    public KModuleBeanFactoryPostProcessor kiePostProcessor() {
//        return new KModuleBeanFactoryPostProcessor();
//    }



//    @Bean
//    public KieContainer kieContainer() {
//        KieServices kieServices = KieServices.Factory.get();
//        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
//        kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile));
//        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
//        kieBuilder.buildAll();
//        KieModule kieModule = kieBuilder.getKieModule();
//        return kieServices.newKieContainer(kieModule.getReleaseId());
//    }
}

